/**
 * 卡券公共方法
 */
import wepy from 'wepy'
import Butils from './../utils/business.utils.js'
export default class CouponMixin extends wepy.mixin {
  formatCouponLimitTime(time) {
    let splitCouponTimeRegexp = /[- :]/
    let timeArr = time.split(splitCouponTimeRegexp)
    return `${timeArr[0]}.${timeArr[1]}.${timeArr[2]}`
  }

  dealStatus(status) {
    let str = ''
    switch (status) {
      case 2:
        str = 'received'
        break
      case 3:
        str = 'used'
        break
      case 4:
        str = 'invalid'
        break
      case 5:
        str = 'unpublish'
        break
      case 6:
        str = 'finish'
        break
      default:
        str = ''
    }
    return str
  }

  dealTempType(type) {
    switch (type) {
      case 0:
        return '限门店和商城使用'
      case 1:
        return '限商城使用'
      case 2:
        return '限门店使用'
    }
  }

  dealTimeLimit(couponObj) {
    let str = ''
    if (couponObj.validDay && couponObj.validDay > 0) {
      str = `领取之日起${couponObj.validDay}天有效`
    } else if (couponObj.activeBegin) {
      // 兼容我的卡券与、套券反馈时间格式不一致：时间戳与日期
      if (typeof couponObj.activeBegin === 'number') {
        couponObj.activeBegin = Butils.stringifyDate(
          new Date(couponObj.activeBegin)
        )
        couponObj.activeEnd = Butils.stringifyDate(
          new Date(couponObj.activeEnd)
        )
      }
      str = `${this.formatCouponLimitTime(
        couponObj.activeBegin
      )} - ${this.formatCouponLimitTime(couponObj.activeEnd)}`
    }
    return str
  }

  dealTextMessage(couponObj) {
    let couponType = ''
    let badgeText = ''
    let couponDiscount = ''
    let subContent = ''
    let suffix = ''
    let prefix = ''

    let type = parseInt(couponObj.couponType)
    switch (type) {
      case 2:
        couponType = 'exchange'
        badgeText = '兑'
        couponDiscount = '兑换券'
        subContent =
          couponObj.subTitle && couponObj.subTitle.length > 10
            ? couponObj.subTitle.substr(0, 10) + '...'
            : couponObj.subTitle
        break
      case 3:
        couponType = 'discount'
        badgeText = '折'
        suffix = '折'
        couponDiscount = parseFloat(couponObj.discountRate * 10).toFixed(1)
        subContent = couponObj.orderAmount
          ? `购物满${(couponObj.orderAmount / 100).toFixed(2)}元可用`
          : '无门槛使用'
        break
      case 4:
        couponType = 'cash'
        badgeText = '代'
        prefix = '¥'
        couponDiscount = parseFloat(couponObj.denomination / 100).toFixed(2)
        subContent = couponObj.orderAmount
          ? `购物满${(couponObj.orderAmount / 100).toFixed(2)}元可用`
          : '无门槛使用'
        break
    }
    return {
      couponType,
      badgeText,
      couponDiscount,
      subContent,
      suffix,
      prefix
    }
  }

  dealStyle(couponType, status) {
    let buiCouponType = couponType ? `bui-coupon-type-${couponType}` : ''
    let buiCouponStatus = status ? `bui-coupon-status-${status}` : ''
    let statusBackground = ''
    switch (status) {
      case 'received':
        statusBackground = '../assets/coupon/icon-received.svg'
        break
      case 'used':
        statusBackground = '../assets/coupon/icon-used.svg'
        break
      case 'invalid':
        statusBackground = '../assets/coupon/icon-invalid.svg'
        break
      case 'unpublish':
        statusBackground = '../assets/coupon/icon-unpublish.svg'
        break
      case 'finish':
        statusBackground = '../assets/coupon/icon-finish.svg'
        break
      case '':
        statusBackground = ''
        break
    }
    return {
      buiCouponType,
      buiCouponStatus,
      statusBackground
    }
  }
}
